import {INIT_LIST_ACTION,CHANGE_INPUT_VALUE,ADD_TODO_ITEM,DEL_TODO_ITEM} from './actionType'

//管理存储数据
const defaultState={
    inputValue:'',
    list:[]
}

//state存储的数据
export default(state = defaultState,action)=>{
    //state上一次的数据，action是store传过来的数据
    // console.log(state,action);

    //异步得到list
    if(action.type === INIT_LIST_ACTION){
        //拷贝一份原始的state进行修改
        const newState = JSON.parse(JSON.stringify(state));
        newState.list = action.value;
        return newState;
    }

    //改变input时改变store
    if(action.type === CHANGE_INPUT_VALUE){
        //拷贝一份原始的state进行修改
        const newState = JSON.parse(JSON.stringify(state));
        newState.inputValue = action.value;
        return newState;
    }

    //点击提交添加到list
    if(action.type === ADD_TODO_ITEM){
        const newState=JSON.parse(JSON.stringify(state));
        newState.list.push( action.value );
        newState.inputValue='';
        return newState;
    }

    //删除list中的项
    if(action.type === DEL_TODO_ITEM){
        const newState=JSON.parse(JSON.stringify(state));
        newState.list.splice(action.index,1);
        return newState;
    }

    return state;
}